home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C ++ / Frameworks / MacZoop 1.6.5 / More Classes / Window Classes / ZTextWindow.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-17  |  5.7 KB  |  388 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************************
  2. *
  3. *
  4. *            ObjectMacZapp        -- a standard Mac OOP application template
  5. *
  6. *
  7. *
  8. *            ZTextWindow.cpp        -- a window that displays text files (uses TextEdit)
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *            © 1996, Graham Cox
  15. *
  16. *
  17. *
  18. *
  19. *************************************************************************************************/
  20.  
  21.  
  22.  
  23. #include    "ZTextWindow.h"
  24. #include    "MacZoop.h"
  25.  
  26.  
  27. ZTextWindow::ZTextWindow(ZCommander* aBoss, short windID, Boolean allowEditing)
  28.     : ZScroller(aBoss, windID, TRUE, TRUE)
  29. {
  30.     itsText = NULL;
  31.     isEditable = allowEditing;
  32. }
  33.  
  34.  
  35.  
  36. ZTextWindow::~ZTextWindow()
  37. {
  38.     if (itsText)
  39.         TEDispose(itsText);
  40. }
  41.  
  42.  
  43. void    ZTextWindow::InitZWindow()
  44. {
  45.     inherited::InitZWindow();
  46.     
  47.     MakeTextEdit();
  48. }
  49.  
  50. void    ZTextWindow::DrawContent()
  51. {
  52.     Rect    r;
  53.     
  54.     SetOrigin( 0, 0 );
  55.     GetContentRect(&r);
  56.     ClipRect( &r );
  57.     TEUpdate(&r, itsText);
  58. }
  59.  
  60. void    ZTextWindow::ClickContent(Point mouse, short modifiers)
  61. {
  62.     if (isEditable)
  63.     {
  64.         SetOrigin( 0, 0 );
  65.         TEClick(mouse, (modifiers & shiftKey), itsText);
  66.     }
  67. }
  68.  
  69. void    ZTextWindow::Activate()
  70. {
  71.     inherited::Activate();
  72.     TEActivate(itsText);
  73. }
  74.  
  75. void    ZTextWindow::Deactivate()
  76. {
  77.     TEDeactivate(itsText);
  78.     inherited::Deactivate();
  79. }
  80.  
  81.  
  82. void    ZTextWindow::Idle()
  83. {
  84.     if (isEditable)
  85.         TEIdle( itsText);
  86. }
  87.  
  88.  
  89. void    ZTextWindow::AdjustCursor( const Point mouse, const short modifiers )
  90. {
  91.     Rect        content;
  92.     CursHandle    aCursor;
  93.     
  94.     if ( isEditable )
  95.     {
  96.         GetContentRect( &content );
  97.         
  98.         if (PtInRect( mouse, &content ))
  99.         {
  100.             aCursor = GetCursor( iBeamCursor );
  101.             SetCursor( *aCursor );
  102.         }
  103.         else
  104.             SetCursor( &qd.arrow );
  105.     }
  106. }
  107.  
  108.  
  109. void    ZTextWindow::UpdateMenus()
  110. {
  111.     if ( isEditable )
  112.     {
  113.         if ((*itsText)->selStart < (*itsText)->selEnd )
  114.         {
  115.             gMenuBar->EnableCommand( kCmdCut );    
  116.             gMenuBar->EnableCommand( kCmdCopy );    
  117.             gMenuBar->EnableCommand( kCmdClear );    
  118.         }
  119.         
  120.         gMenuBar->EnableCommand( kCmdSelectAll );    
  121.     }
  122.     
  123.     inherited::UpdateMenus();
  124. }
  125.  
  126.  
  127.  
  128. Boolean    ZTextWindow::CanPasteType()
  129. {
  130.     return isEditable && gClipboard->QueryType( 'TEXT' );
  131. }
  132.  
  133.  
  134. void    ZTextWindow::DoCut()
  135. {
  136.     if ( isEditable )
  137.     {
  138.         gClipboard->Clear();
  139.         TECut( itsText );
  140.         TEToScrap();
  141.         RecalText();
  142.     }
  143. }
  144.  
  145.  
  146. void    ZTextWindow::DoCopy()
  147. {
  148.     if ( isEditable )
  149.     {
  150.         gClipboard->Clear();
  151.         TECopy( itsText );
  152.         TEToScrap();    
  153.     }
  154. }
  155.  
  156.  
  157. void    ZTextWindow::DoPaste()
  158. {
  159.     if ( isEditable )
  160.     {
  161.         TEFromScrap();
  162.         TEPaste( itsText );
  163.         RecalText();
  164.     }
  165. }
  166.  
  167.  
  168. void    ZTextWindow::DoClear()
  169. {
  170.     if ( isEditable )
  171.     {
  172.         TEDelete( itsText );
  173.         RecalText();
  174.     }
  175. }
  176.  
  177.  
  178. void    ZTextWindow::DoSelectAll()
  179. {
  180.     if ( isEditable )
  181.         TESetSelect( 0, 32767, itsText );
  182.  
  183. }
  184.  
  185.  
  186.  
  187. void    ZTextWindow::SetSize(short width, short height)
  188. {
  189.     inherited::SetSize( width, height );
  190.     RecalText();
  191. }
  192.  
  193.  
  194. void    ZTextWindow::Zoom(short partCode)
  195. {
  196.     inherited::Zoom(partCode);
  197.     RecalText();
  198.     Draw();
  199. }
  200.  
  201.  
  202.  
  203. void    ZTextWindow::Scroll(short dH, short dV)
  204. {
  205.     TEPinScroll(dH, dV, itsText);
  206. }
  207.  
  208.  
  209.  
  210. void    ZTextWindow::Type(char theChar)
  211. {
  212.     if (isEditable)
  213.     {
  214.         TEKey(theChar, itsText);
  215.         
  216.         dirty = TRUE;
  217.     }
  218. }
  219.  
  220.  
  221. void    ZTextWindow::MakeTextEdit()
  222. {
  223.     Rect    destRect, viewRect;
  224.     
  225.     // the dest rect is the scrollbounds, but that hasn't been set yet.
  226.     // the view rect is the content area
  227.     
  228.     Focus();
  229.     
  230.     TextFont(monaco);
  231.     TextSize(9);
  232.     
  233.     emSpace = CharWidth('m');
  234.     GetContentRect(&viewRect);
  235.     
  236.     destRect = viewRect;
  237.     destRect.right = destRect.left + (emSpace * 255);
  238.     
  239.     FailNIL(itsText = TEStyleNew(&destRect, &viewRect));
  240.     
  241.     SetBounds( destRect );
  242.     RecalText();
  243. }
  244.  
  245.  
  246.  
  247. void    ZTextWindow::GetContentRect(Rect* contents)
  248. {
  249.     inherited::GetContentRect(contents);
  250.     
  251.     contents->top += 2;
  252.     contents->left += 2;
  253. }
  254.  
  255.  
  256. void    ZTextWindow::OpenFile(OSType fType)
  257. {
  258.     FInfo    fi;
  259.     short    refNum;
  260.     long    pSize;
  261.     Handle    temp = NULL;
  262.  
  263.     if (macFile.vRefNum != kNoFile)
  264.     {
  265.         FailOSErr(FSpGetFInfo(&macFile, &fi));
  266.         
  267.         if (fi.fdType != 'TEXT')
  268.             FailOSErr(paramErr);
  269.             
  270.         FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum ));
  271.         
  272.         try
  273.         {
  274.             FailOSErr(GetEOF(refNum, &pSize));
  275.             if (pSize > kMaxTextSize)
  276.                 FailOSErr(kTextFileTooBigErr);
  277.                 
  278.             FailNIL(temp = NewHandle(pSize));    
  279.             
  280.             HLock(temp);
  281.             FailOSErr(FSRead(refNum, &pSize, *temp));
  282.             
  283.             // set the text in text edit
  284.             
  285.             TESetText(*temp, pSize, itsText);
  286.             HUnlock(temp);
  287.             
  288.             DisposeHandle(temp);
  289.             FSClose(refNum);
  290.         }
  291.         catch(OSErr err)
  292.         {
  293.             FSClose(refNum);
  294.             
  295.             if (temp)
  296.             {
  297.                 HUnlock(temp);
  298.                 DisposeHandle(temp);
  299.             }
  300.             throw err;
  301.         }
  302.     }    
  303.     inherited::OpenFile( fType );
  304.     
  305.     // set up the bounds, etc
  306.     
  307.     RecalText();
  308.     Draw();
  309. }
  310.  
  311.  
  312.  
  313. void    ZTextWindow::SaveFile()
  314. {
  315.     short        refNum;
  316.     long        pSize;
  317.     OSErr        theErr;
  318.     Handle        text = NULL;
  319.     char        tState;
  320.     
  321.     if (macFile.vRefNum != kNoFile)
  322.     {
  323.         theErr = FSpOpenDF(&macFile, fsCurPerm, &refNum);
  324.         
  325.         if (theErr == fnfErr)
  326.         {
  327.             FailOSErr(FSpCreate(&macFile, gAppSignature, 'TEXT', 0));
  328.             FailOSErr(FSpOpenDF(&macFile, fsCurPerm, &refNum));
  329.         }
  330.         else
  331.             FailOSErr(theErr);
  332.         
  333.         try
  334.         {
  335.             FailNIL(text = (*itsText)->hText);
  336.             
  337.             pSize = GetHandleSize(text);
  338.                 
  339.             tState = HGetState(text);
  340.             HLock(text);
  341.             FailOSErr(FSWrite(refNum, &pSize, *text));
  342.             HSetState(text, tState);
  343.             
  344.             FailOSErr(SetEOF(refNum, pSize));
  345.             FSClose(refNum);    
  346.         }
  347.         catch(OSErr err)
  348.         {
  349.             FSClose(refNum);
  350.             if (text)
  351.                 HSetState(text, tState);
  352.             throw err;
  353.         }
  354.         inherited::SaveFile();
  355.     }
  356. }
  357.  
  358.  
  359. void    ZTextWindow::RecalText()
  360. {
  361.     static Boolean rtInProgress = FALSE;
  362.     
  363.     Rect    content,tBounds;
  364.     short     textHeight;
  365.     short    lineHeight;
  366.     
  367.     if ( ! rtInProgress )
  368.     {
  369.         rtInProgress = TRUE;
  370.         
  371.         GetContentRect(&content);
  372.         (*itsText)->viewRect = content;
  373.         
  374.         TECalText(itsText);    
  375.         lineHeight = TEGetHeight(1, 1, itsText);
  376.         textHeight = TEGetHeight(0, 32767 , itsText);
  377.         
  378.         GetBounds(&tBounds);
  379.         tBounds.bottom = textHeight + lineHeight;
  380.         SetBounds( tBounds );
  381.         
  382.         (*itsText)->destRect = tBounds;
  383.         SetScrollAmount( emSpace, lineHeight);
  384.         
  385.         rtInProgress = FALSE;
  386.     }
  387. }
  388.